<?php
//this file change the frequency matrix to tf-idf matrix
if ($argc < 2) {
    die("Usage: php convertFrequencyToTFIDF.php srcFile dstFile\n");
}
else {
    $srcFile = $argv[1];
    $dstFile = $argv[2];
}


set_time_limit(0);
ini_set('memory_limit','3048M');

if (!$src_handle = fopen($srcFile, 'r+')) {
    echo "不能打开文件 $srcFile";
    exit;
}
$fMatrix = array();
while (!feof($src_handle)) {
    $buffer = fgets($src_handle, 4096);
    $itemInfo = explode(" ", $buffer);
    $fMatrix[$itemInfo[0]][$itemInfo[1]] = trim($itemInfo[2]);
}
fclose($src_handle);
$totalDocNum = count($fMatrix);
$docTermNum = array();
foreach($fMatrix as $docId => $terms) {
    $docTermNum[$docId] = 0;
    foreach ($terms as $termId => $frequency) {
        $docTermNum[$docId] += $frequency;
        if (empty($termDocNum[$termId])) {
            $termDocNum[$termId] = 1;
        }
        else {
            ++$termDocNum[$termId];
        }
    }
    unset($terms);
}
if (!$dst_handle = fopen($dstFile, 'w+')) {
    echo "不能打开文件 $dstFile";
    exit;
}

$tfidfMatrix = array();
foreach($fMatrix as $docId => $terms) {
    foreach ($terms as $termId => $frequency) {
        if (empty($idf[$termId])) {
            $idf[$termId] = log($totalDocNum*1.0/$termDocNum[$termId]);
            unset($termDocNum[$termId]);
        }
        if ($docTermNum[$docId] < 0.00000001) {
            $tf_idf = 0;
        }
        else {
            $tf_idf = round(($frequency/$docTermNum[$docId]) * $idf[$termId],5);
        }
        if(!empty($docId) && !empty($termId)) fwrite($dst_handle, $docId." ".$termId." ".$tf_idf."\n");
    }
}
fclose($dst_handle);
